<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 调度星盘ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

//引入查询订单主表信息类
quanli_require('module/base/bill/billSearch.class.php');

//查询配送员类
quanli_require('module/base/system/sysDistributionTechnician.class.php');

class ajax_schedulingChart {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1:
                    $this->schedulingChart();//查询显示配送站地图标注信息
                    break;

                case 2:
                    $this->sysDistribution_technician();//弹出层中根据配送员id查询配送员手机号

                case 3:
                    $this->editDistribution_technician();//修改配送员
            }
        }
    }

    /**
     * 查询显示配送站地图标示信息
     */
    private function schedulingChart() {

        global $G_PGLOB;

        //实例订单主表类
        $billMaster = new billSearch();

        if( !isset( $_REQUEST['formValues']['send_date'], $_REQUEST['formValues']['deliver_cityid'], $_REQUEST['formValues']['distribution_id']) ){

            returnJsonData('缺少参数',400);
        }

        if( !is_date( $_REQUEST['formValues']['send_date'] ) ){
            returnJsonData('时间格式错误',400);
        }

        if( !is_numeric( $_REQUEST['formValues']['deliver_cityid']) || !is_numeric($_REQUEST['formValues']['distribution_id']) ){

            returnJsonData('参数错误',400);
        }

        //查询条件选择了城市、配送站 则根据选择的城市、配送站查询
        if( isset($_REQUEST['formValues']['cityID']) && is_numeric($_REQUEST['formValues']['cityID']) && isset( $_REQUEST['formValues']['distribution_id']) && is_numeric($_REQUEST['formValues']['distribution_id'])){
            $_REQUEST['formValues']['cityID'] = $_REQUEST['formValues']['cityID'];
            $_REQUEST['formValues']['distribution_id'] = $_REQUEST['formValues']['distribution_id'];

            //查询条件选择了城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的配送站查询
        }else if(isset($_REQUEST['formValues']['cityID']) && is_numeric($_REQUEST['formValues']['cityID']) && !isset( $_REQUEST['formValues']['distribution_id']) || !is_numeric($_REQUEST['formValues']['distribution_id']) ){
            $_REQUEST['formValues']['cityID'] = $_REQUEST['formValues']['cityID'];
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['formValues']['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['formValues']['distribution_id'] = $distribution_id_array[0];

            }

            //查询条件没有选择城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的城市、配送站查询
        }else{
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['formValues']['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['formValues']['distribution_id'] = $distribution_id_array[0];

            }
        }

        //查询订单主表信息
        $billMaster_array = $billMaster->searchBillMaster( $_REQUEST['formValues'] );

        if(!$billMaster_array){
            returnJsonData( '未找到对应订单信息',400 );
        }

        $bill_goods_count =array();//订单统计数据数组
        $bill_goods_count['count_bill_money'] = 0;
        $bill_goods_count['count_bill_receivable_money'] =0;
        $bill_goods_count['count_bill_totalAmount'] =0;
        $bill_goods_count['already_count_bill_totalAmount'] = 0;
        $bill_goods_count['not_count_bill_totalAmount']=0;
        $bill_goods_count['already_count_goods_spec'] = 0;
        $bill_goods_count['not_count_goods_spec'] =0;

        foreach( $billMaster_array as $key=>$val ){

            //过滤顺丰马卡龙订单 Add By allen.qiang 2015-06-25
            if(date('Y-m-d') > '2015-05-06' && $val['billPromotion']=='495' )
                continue;

            $billMaster_array[$key]['bill_code_sub'] = substr( $val['bill_code'], -6);
            $bill_goods_count['distribution_name'] =$val['distribution_name'];//配送站
            $bill_goods_count['count_bill_money'] +=$val['bill_money'];//订单总金额
            $bill_goods_count['count_bill_receivable_money'] +=$val['bill_receivable_money'];//订单应收金额
            $bill_goods_count['count_bill_totalAmount'] += 1;//订单总数
            if( $val['distribution_technician_id'] > 0){
                $bill_goods_count['already_count_bill_totalAmount'] += 1;//已分配总数

            }else{
                $bill_goods_count['not_count_bill_totalAmount'] += 1;//未分配总数
            }


            //查询订单统计数据
            $arg = array(
                'bill_no'=>$val['bill_no']
            );
            $bill_goods_array = $billMaster->searchBillGoods( $arg );

            if( $bill_goods_array ){

                foreach( $bill_goods_array as $v){
                    $spec_substr = substr( $v['goods_spec'],-3 );

                    if( $spec_substr == '磅' ){

                        if( $val['distribution_technician_id'] > 0){
                            $already_spec_num = substr( $v['goods_spec'],0,-3 );
                            $bill_goods_count['already_count_goods_spec'] += $already_spec_num;//已分配磅数
                        }else{
                            $not_spec_num = substr( $v['goods_spec'],0,-3 );
                            $bill_goods_count['not_count_goods_spec'] += $not_spec_num;//未分配磅数
                        }

                    }

                }

            }

            if( !isset($bill_goods_count['already_count_goods_spec'])){
                $bill_goods_count['already_count_goods_spec'] =0;
            }
            if( !isset($bill_goods_count['not_count_goods_spec'])){
                $bill_goods_count['not_count_goods_spec'] =0;
            }


        }

//        echo '<pre>';
//        print_r( $billMaster_array );exit;
        $return_array = array(
            'billMaster' =>$billMaster_array,
            'bill_goods_count' =>$bill_goods_count
        );
        returnJsonData( 'ok', 200, $return_array );

    }


    /*
    * 弹出层中根据配送员id查询配送员手机号
    *
    */
    public function sysDistribution_technician(){
        global $G_PGLOB;

        if(!isset($_REQUEST['distribution_technician_id'])){
            returnJsonData('缺少参数',400);
        }

        if(!is_numeric($_REQUEST['distribution_technician_id'])){
            returnJsonData('参数错误',400);
        }

        //实例化配送员类
        $sysDistributionTechnician = new sysDistributionTechnician();

        $arg_array =  array(
            'distribution_technician_id'=>$_REQUEST['distribution_technician_id']
        );
        $result = $sysDistributionTechnician->search( $arg_array );

        if($result){
            returnJsonData('ok',200,$result);
        }else{
            returnJsonData('未找到配送员信息',400);
        }
    }

    /*
     *
     * 修改配送员
     */
    public function editDistribution_technician(){

        global $G_PGLOB;

        if(!isset($_REQUEST['distribution_technician_id'], $_REQUEST['bill_no'], $_REQUEST['distribution_technicia'])){

            returnJsonData('缺少参数',400);
        }

        if(!is_numeric($_REQUEST['distribution_technician_id'])){
            returnJsonData('参数错误',400);
        }

        //实例化配送员类 校验配送员信息
        $sysDistributionTechnician = new sysDistributionTechnician();

        $arg_array =  array(
            'distribution_technician_id'=>$_REQUEST['distribution_technician_id'],
            'mobile'=>$_REQUEST['mobile']
        );
        $result = $sysDistributionTechnician->search( $arg_array );

        if(!$result){
            returnJsonData('未找到对应配送员信息',400);
        }

        //实例化订单主表
        $bill_master_obj = new entity('bill_master');
        $bill_master_obj->bill_no =$_REQUEST['bill_no'];
        if(!$G_PGLOB->DBCA->fetch($bill_master_obj)){
            returnJsonData('未找到该订单主表信息',400);
        }
        if( $bill_master_obj->distribution_technicia ){
            $old_distribution_technicia = $bill_master_obj->distribution_technicia;
        }else{
            $old_distribution_technicia = '';
        }
        $bill_master_obj->distribution_technician_id = $_REQUEST['distribution_technician_id'];
        $bill_master_obj->distribution_technicia = $_REQUEST['distribution_technicia'];
        $G_PGLOB->DBCA->save($bill_master_obj);

        /*=======================================================
         * 写入日志 start
         * ======================================================
         */
        quanli_include('module/base/customer/logHandler.class.php');
        //实例日志类
        $logHandler = new logHandler( $G_PGLOB->DBCA );



        $log_array = array(
            'logCategoryID' => 1,
            'logItem' => $bill_master_obj->bill_no,
            'extend_1' => '调度星盘设置',
            'extend_2' => $old_distribution_technicia?'配送员由【'.$old_distribution_technicia.'】设置为【'.$bill_master_obj->distribution_technicia.'】':'配送员设置为【'.$bill_master_obj->distribution_technicia.'】',
            'extend_5' => $bill_master_obj->bill_status,
            //mod by allen.qiang 20141029 系统中操作信息中“操作人”切换为中文--start
            //'operator' => $G_PGLOB->currentUserID
            'operator' => $G_PGLOB->currentUserName
            //mod by allen.qiang 20141029 系统中操作信息中“操作人”切换为中文--end
        );

        $result_array = $logHandler->saveLogInfo( $log_array );
        if ($result_array['code'] != 200){
            returnJsonData( $result_array['message'], 400);
        }

        /*=======================================================
         * 写入日志 end
         * ======================================================
         */

        returnJsonData( '配送员修改成功', 200 );

    }

}